/***(LcdText.m)****************************************************************
*H* LcdText implementation of Text Subclass 	  	    	    	      *
******************************************************************************/

#import <appkit/Pasteboard.h>
#import <appkit/Font.h>

#import "LcdText.h"
#import "Calculator.h"

void UTL_NxDumpEvent (NXEvent*, int, int, int);

@implementation LcdText

/******************************************************************************
* FUNCTION: NoCharFilter    	    	    	    	    	    	      *
******************************************************************************/
unsigned short NoCharFilter

    (/* Arguments */
    unsigned short  theChar,
    int     	    flags,
    unsigned short  charSet)

{
return(theChar);
}



/******************************************************************************
* INSTANCE METHOD:- initFrame:	    	    	    	    	    	      *
*   This  method  overrides  its  super  class  to  define  specific  initial *
* attributes of a Text object. 	    	    	    	    	    	      *
******************************************************************************/
- initFrame

    /* Arguments */
    :(NXRect *)frameRect

{   /* Local Variables */
    NXRect sizeRect;

/* BEGIN-initFrame: */
[super initFrame:frameRect];

/* Establish the font characteristics for this Text Object */
[self setMonoFont:YES];
[self setFont:[Font newFont:"Courier-Bold" size:14.0
    	            style:0 matrix:NX_FLIPPEDMATRIX]];
[self setBackgroundGray:NX_WHITE];
[self setOpaque:YES];

/* Set Text for resizing */
[self notifyAncestorWhenFrameChanged: YES];
[self setVertResizable:YES];

/* Set size restrictions appropriate for a scrolling Text Object */
sizeRect = *frameRect;
[self setMinSize:&sizeRect.size];
sizeRect.size.height = 1.0e30;
[self setMaxSize:&sizeRect.size];

[self setCharFilter:NoCharFilter];

/* Initial the text position of the beginning of the current line */
IVstartOfCurLine = 0;

return(self);
}/* END-initFrame: */



/******************************************************************************
* INSTANCE METHOD:- keyDown:	    	    	    	    	    	      *
*   This method captures any keystrokes  that  would have been interpreted by *
* the LcdText Object and diverts  them  for  interpretation by the Calculator *
* object.   	    	    	    	    	    	    	    	      *
******************************************************************************/
- keyDown

    /* Arguments */
    :(NXEvent *)event

{/* BEGIN-keyDown: */
#ifdef DEBUG
printf("LcdText-keyDown:");
UTL_NxDumpEvent (event, 1, 1, 1);
#endif

/* Give event to the Calculator Object */
[Calculator KeyBoardEquivalent:event];

return(self);
}/* END-keyDown: */



/******************************************************************************
* INSTANCE METHOD:- paste:  		    	    	    	    	      *
*   This method overides its super class to control what can be pasted to the *
* LcdText object.  This  method  yanks the ASCII data from the Pasteboard and *
* sends it to the Calculator Object for parsing. This restricts the text that *
* can be pasted to the LcdTextObject.	    	    	    	    	      *
******************************************************************************/
- paste:sender

{   /* Local Variables */
    id	    	  pboard;
    char    	 *pbData;
    int	    	  pbLength;
    const NXAtom *pbTypes;

/* BEGIN-paste: */
/* Query the Global Pasteboard Object for Id and Data Types */
pboard  = [Pasteboard newName:NXGeneralPboard];
pbTypes = [pboard types];

if ( [pboard readType:NXAsciiPboardType data:&pbData length:&pbLength] )
    {/* Use the data here, keeping it for as long as necessary */
    [Calculator PasteExpression:pbData Length:pbLength];
    [pboard deallocatePasteboardData:pbData length:pbLength];
    }

return(self);
}/* END-paste: */



/******************************************************************************
* INSTANCE METHOD:- newLine:	        	    	    	    	      *
*   This method places a new line at the end of the current line.   	      *
******************************************************************************/
- newLine:sender

{   /* Local Variables */
    int  endOfText;

/* BEGIN-newLine: */
endOfText = [self textLength];
[self setSel:endOfText :endOfText];
[self replaceSel:"\n"];
IVstartOfCurLine = endOfText+1;
return(self);
}/* END-newLine: */



/******************************************************************************
* INSTANCE METHOD:- setCurrentLine:	    	    	    	    	      *
*   This method replaces the current line with the supplied text.   	      *
******************************************************************************/
- setCurrentLine

    /* Arguments */
    :(const char*)string    /* String to replace current line */

{   /* Local Variables */
    int  endOfText;

/* BEGIN-setCurrentLine: */
/* Turn off updates until finished mangling the text */
[self setAutodisplay:NO];

/* Replace the text from the begining to the end of the current line */
endOfText = [self textLength];
[self setSel:IVstartOfCurLine :endOfText];
[self scrollSelToVisible];
[self replaceSel:string];

/* Restore updates and redisplay the Text object */
[[self setAutodisplay:YES] display];

return(self);
}/* END-setCurrentLine: */



/******************************************************************************
* INSTANCE METHOD:- appendCurrentLine:	    	    	    	    	      *
*   This method appends the supplied text to the end of the current line.     *
******************************************************************************/
- appendCurrentLine

    /* Arguments */
    :(const char*)string    /*I* String to Append to current line */

{   /* Local Variables */
    int  endOfText;

/* BEGIN-appendCurrentLine: */
/* Turn off updates until finished mangling the text */
[self setAutodisplay:NO];

/* Add new text at the end of the current line */ 
endOfText = [self textLength];
[self setSel:endOfText :endOfText];
[self scrollSelToVisible];
[self replaceSel:string];

/* Restore updates and redisplay the Text object */
[[self setAutodisplay:YES] display];

return(self);
}/* END-appendCurrentLine: */



/******************************************************************************
* INSTANCE METHOD:- setTarget:Action:	    	    	    	    	      *
*   This method establishes an object to receive the action method specified. *
******************************************************************************/
- setTarget

    /* Arguments */
          :anObject
    Action:(SEL)aSelector

{/* BEGIN-setTarget:Action: */
IVactionId  = anObject;
IVactionSel = aSelector;
return(self);
}/* END-setTarget:Action: */


#if 0
/******************************************************************************
* INSTANCE METHOD:- textDidChange:  	    	    	    	    	      *
******************************************************************************/
- textDidChange:sender

{/* BEGIN-textDidChange: */
printf("LcdText:-textDidChange:\n");
return(self);
}/* END-textDidChange: */



/******************************************************************************
* INSTANCE METHOD:- textWillChange: 	    	    	    	    	      *
******************************************************************************/
- (BOOL)textWillChange:sender

{/* BEGIN-textWillChange: */
printf("LcdText:-textWillChange:\n");
return(YES);
}/* END-textWillChange: */
#endif

@end /* implementation LcdText */
